﻿<template>
  <a-modal
    title="新增数据备份"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="备份记录ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入备份记录ID" v-decorator="['stepRecordId']" />
        </a-form-item>
        <a-form-item label="备份时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择备份时间" v-decorator="['executeTime']" @change="onChangeexecuteTime"/>
        </a-form-item>
        <a-form-item label="备份类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择备份类型" v-decorator="['backupType', {rules: [{ required: true, message: '请选择备份类型！' }]}]">
            <a-select-option v-for="(item,index) in backupTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备份错误消息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入备份错误消息" v-decorator="['backupErrorMessage']" />
        </a-form-item>
        <a-form-item label="压缩备份" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isCompress', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="远程存储错误消息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入远程存储错误消息" v-decorator="['remoteStoreErrorMessage']" />
        </a-form-item>
        <a-form-item label="未测试的指向0" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入未测试的指向0" v-decorator="['testStepRecordId']" />
        </a-form-item>
        <a-form-item label="还原测试状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择还原测试状态" v-decorator="['testStatus', {rules: [{ required: true, message: '请选择还原测试状态！' }]}]">
            <a-select-option v-for="(item,index) in testStatusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="最后测试时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择最后测试时间" v-decorator="['testTime']" @change="onChangetestTime"/>
        </a-form-item>
        <a-form-item label="备份测试错误消息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入备份测试错误消息" v-decorator="['testErrorMessage']" />
        </a-form-item>
        <a-form-item label="测试所数据库服务器" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入测试所数据库服务器" v-decorator="['testServerId']" />
        </a-form-item>
        <a-form-item label="存放文件夹路径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入存放文件夹路径" v-decorator="['storePathLocal']" />
        </a-form-item>
        <a-form-item label="远程存储ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入远程存储ID" v-decorator="['remoteStoreId']" />
        </a-form-item>
        <a-form-item label="远程存放路径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入远程存放路径" v-decorator="['storePathRemote']" />
        </a-form-item>
        <a-form-item label="删除时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择删除时间" v-decorator="['deleteTime']" @change="onChangedeleteTime"/>
        </a-form-item>
        <a-form-item label="本地已删除" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isLocalBackupDeleted', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="远程已删除" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isRemoteBackupDeleted', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="未删除的指向0" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入未删除的指向0" v-decorator="['cleanStepRecordId']" />
        </a-form-item>
        <a-form-item label="数据库服务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库服务ID" v-decorator="['dbServerId']" />
        </a-form-item>
        <a-form-item label="数据库ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库ID" v-decorator="['dbId']" />
        </a-form-item>
        <a-form-item label="月末备份" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isMonthly', { valuePropName: 'checked' }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    DbBackupAdd
  } from '@/api/modular/main/DbBackupManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        executeTimeDateString: '',
        backupTypeData: [],
        testStatusData: [],
        testTimeDateString: '',
        deleteTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	// 初始化方法
	add (record) {
	this.visible = true
	const backupTypeOption = this.$options
	this.backupTypeData = backupTypeOption.filters['enumData']('BackupType')
	const testStatusOption = this.$options
	this.testStatusData = testStatusOption.filters['enumData']('TestStatus')
	},
	/**
	* 提交表单
	*/
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	}
	}
	values.executeTime = this.executeTimeDateString
	values.testTime = this.testTimeDateString
	values.deleteTime = this.deleteTimeDateString
	DbBackupAdd(values).then((res) => {
	if (res.success) {
	this.$message.success('新增成功')
	this.confirmLoading = false
	this.$emit('ok', values)
	this.handleCancel()
	} else {
	this.$message.error('新增失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	onChangeexecuteTime(date, dateString) {
	this.executeTimeDateString = dateString
	},
	onChangetestTime(date, dateString) {
	this.testTimeDateString = dateString
	},
	onChangedeleteTime(date, dateString) {
	this.deleteTimeDateString = dateString
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
